﻿using System.Net.Mime;
using OsPortal;
using System;
using System.Data;
using System.Net;
using System.Net.Mail;

public class clsEmail
{
    private static string strClassName = "clsEmail";
    private const string FacebookMail = "@facebook.com";

    public static bool sendMail(string strUserName, string strPassWord, string strName, string strFrom, string strTo, string strSubject, string strContent, string strTypeMail)
    {
        try
        {
            MailMessage message = new MailMessage
            {
                From = new MailAddress(strFrom, strName)
            };
            if (strTo.Contains(FacebookMail))
                message.Bcc.Add(new MailAddress(strTo));
            else
                message.To.Add(strTo);
            message.Subject = strSubject;
            message.IsBodyHtml = true;
            message.Body = strContent;
            message.Priority = MailPriority.Normal;
            SmtpClient client = new SmtpClient(strTypeMail, 0x24b);
            NetworkCredential credential = new NetworkCredential(strUserName, strPassWord);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //message.CC.Add(new MailAddress(txtCopy.Text));
            
            //message.Attachments.Add(new Attachment(txtFile.Text, MediaTypeNames.Application.Octet));
            client.Credentials = credential;
            client.EnableSsl = true;
            client.Send(message);
            return true;
        }
        catch (Exception exception)
        {
            oFileHelper.WriteLogErr(strClassName, "sendMail", exception.ToString());
            return false;
        }
    }

    public static bool sendMailOK(string strMailTo, string strSubject, string strContent)
    {
        try
        {
            DataTable table = new DataTable();
            table.ReadXml("AcountConfig.xml");
            if (table == null)
            {
                return false;
            }
            if (table.Rows.Count < 0)
            {
                return false;
            }
            string strUserName = table.Rows[0][0].ToString();
            string strPassWord = table.Rows[0][1].ToString();
            string strTypeMail = table.Rows[0][2].ToString();
            string strName = table.Rows[0][3].ToString();
            string strFrom = table.Rows[0][4].ToString();
            return sendMail(strUserName, strPassWord, strName, strFrom, strMailTo, strSubject, strContent, strTypeMail);
        }
        catch (Exception exception)
        {
            oFileHelper.WriteLogErr(strClassName, "sendMailOK", exception.ToString());
            return false;
        }
    }
}

